package com.gxa.controller.cm;

import com.github.pagehelper.PageInfo;
import com.gxa.param.cm.QueryParam;
import com.gxa.pojo.cm.Customer;
import com.gxa.pojo.cm.vo.TableVO;
import com.gxa.service.cm.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author Glenn
 */
@Controller
@RequestMapping("/cm/customer")
public class CustomerController {

    @Resource
    CustomerService customerService;

    @GetMapping("/list/page")
    public String list() {
        return "cm/customer-list";
    }

    @PostMapping("/list/data")
    @ResponseBody
    public TableVO listData(QueryParam queryParam) {
        PageInfo<Customer> customerPageInfo = customerService.listCustomer(queryParam);
        TableVO tableVO = new TableVO();
        tableVO.setCode(0)
                .setMsg("成功")
                .setCount(customerPageInfo.getTotal())
                .setData(customerPageInfo.getList());
        return tableVO;
    }

    /**
     * 更新客户状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus")
    @ResponseBody
    public TableVO updateStatus(Integer id, Integer status) {
        customerService.updateStatus(id, status);
        return new TableVO().setCode(200).setMsg("OK");
    }

    @GetMapping("/add/page")
    public String add() {
        return "cm/customer-add";
    }

    @PostMapping("/add/data")
    @ResponseBody
    public TableVO addData(Customer customer, HttpSession session) {
        return customerService.addCustomer(customer, session);
    }

    @GetMapping("/edit/page")
    public String edit(Integer id, Model model) {
        Customer customer = customerService.getById(id);
        model.addAttribute("customer", customer);
        return "cm/customer-edit";
    }

    @PostMapping("/updateCustomer")
    @ResponseBody
    public TableVO updateCustomer(Customer customer, HttpSession session) {
        return customerService.updateCustomer(customer, session);
    }

    @GetMapping("/detail/page")
    public String detail(Integer id, Model model) {
        Customer customer = customerService.getById(id);
        model.addAttribute("customer", customer);
        return "cm/customer-detail";
    }

}
